package my.consumer.demo.controller;

import my.consumer.demo.Feign.UserFeignClient;
import my.consumer.demo.Feign.UserZuulFeignClient;
import my.consumer.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("feign")
public class UserFeignController {
    @Autowired
    private UserFeignClient client;
    @Autowired
    private UserZuulFeignClient zuulClient;

    /**
     *      使用Feign方式替换restTemplate方式
     * restTemplate使用起来没有任何，但是会产生很多冗余代码
     * 使用feign方式可以将接口统一管理起来，统一定义熔断方法
     */

    @GetMapping("getUser")
    public User getUser(@RequestParam String userId) {
        return client.getUser(userId);
    }

    @PostMapping("addUser")
    public String addUser(@RequestBody User user) {
        return client.addUser(user);
    }

    /**
     *  这里仅用作测试zuul，加上了token的参数
     *  当token传null或者空字符串，会打印fallback
     *  正确zuul用法应该是搭配nginx
     *  nginx代理相对应请求路径，然后转发到对应的zuul服务
     *  例：
     *  upstram userzuul{
     *      server 127.0.0.1:8901;
     *      #↓假如有多个zuul服务↓
     *      server 127.0.0.1:8902;
     *  }
     *
     *  server{
     *      listen 80;
     *      location /user {
     *          proxy_pass http://userzuul;
     *          proxy_set_header Host $host:8901:
     *      }
     *  }
     */

    @GetMapping("getUserOverZuul")
    public User getUserOverZuul(@RequestParam String userId,@RequestParam String token) {
        return zuulClient.getUser(userId,token);
    }

    @PostMapping("addUserOverZuul")
    public String addUserOverZuul(@RequestBody User user,@RequestParam String token) {
        return zuulClient.addUser(user,token);
    }
}
